//
//  TGMDefaultActivityPullToRefreshView.m
//  TravelGuideMdd
//
//  Created by HanFeng on 2/12/14.
//  Copyright (c) 2014 mafengwo.com. All rights reserved.
//

#import "TGMDefaultActivityPullToRefreshView.h"
#import "TGMActivityIndicatorView.h"

#define TEXT_COLOR	 [UIColor colorWithRed:120.0/255.0 green:120.0/255.0 blue:120.0/255.0 alpha:1.0]
//#define FLIP_ANIMATION_DURATION 0.18f

#define INDICATOR_CENTER_X 160
#define INDICATOR_CENTER_Y 25
#define INDICATOR_CENTER_BOTTOM 34

#define SCOLL_SCALE_START_Y  38
#define ScollScaleView_Size 15
#define ScollScaleView_SemiSize (ScollScaleView_Size * 0.5)

#define HIGHT_LIGHT_COLOR C_RGB(0x282828)

@interface __tgm_default_activity_ScrollScaleView : UIView
{
    UIView *_viewLT;
    UIView *_viewRT;
    UIView *_viewLB;
    UIView *_viewRB;
}

@end

@implementation __tgm_default_activity_ScrollScaleView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        _viewLT = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
        _viewLT.backgroundColor = HIGHT_LIGHT_COLOR;
        [self addSubview:_viewLT];
        
        _viewRT = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
        _viewRT.backgroundColor = HIGHT_LIGHT_COLOR;
        [self addSubview:_viewRT];
        
        _viewLB = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
        _viewLB.backgroundColor = HIGHT_LIGHT_COLOR;
        [self addSubview:_viewLB];
        
        _viewRB = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
        _viewRB.backgroundColor = HIGHT_LIGHT_COLOR;
        [self addSubview:_viewRB];
        
        [self reset];
    }
    
    return self;
}

- (void)reset
{
    CGPoint initialCenter = CGPointMake(self.frameWidth * .5, self.frameHeight * .5);
    _viewLT.center = initialCenter;
    _viewRT.center = initialCenter;
    _viewLB.center = initialCenter;
    _viewRB.center = initialCenter;
}

- (CGPoint)centerOfFactorPercent:(CGFloat)percent directionX:(int)dirX directionY:(int)dirY
{
    CGFloat deltaX = dirX * 2.7 * percent + ScollScaleView_SemiSize;
    CGFloat deltaY = dirY * 2.7 * percent + ScollScaleView_SemiSize;
    
    return CGPointMake(deltaX , deltaY);
}

- (void)scaleRaletaivePercent:(CGFloat)percent;
{
    CGPoint deltaCenter;
    
    //左上
    deltaCenter = [self centerOfFactorPercent:percent directionX:-1 directionY:-1];
    _viewLT.center = deltaCenter;
    
    //右上
    deltaCenter = [self centerOfFactorPercent:percent directionX:1 directionY:-1];
    _viewRT.center = deltaCenter;
    
    //左下
    deltaCenter = [self centerOfFactorPercent:percent directionX:-1 directionY:1];
    _viewLB.center = deltaCenter;
    
    //右下
    deltaCenter = [self centerOfFactorPercent:percent directionX:1 directionY:1];
    _viewRB.center = deltaCenter;
}
@end

@interface TGMDefaultActivityPullToRefreshView () {
    UILabel *_statusLabel;
    TGMActivityIndicatorView *_activityView;
}

@end

@implementation TGMDefaultActivityPullToRefreshView

- (id)initWithFrame:(CGRect)frame
{
    frame.size.width = [[UIScreen mainScreen]bounds].size.width;
    self = [super initWithFrame:frame];
    if (self) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 16.0f, self.frame.size.width, 12.0f)];
        label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        label.font = [UIFont systemFontOfSize:12.0f];
        label.textColor = HIGHT_LIGHT_COLOR;
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:label];
        _statusLabel=label;
        
        TGMActivityIndicatorView *activityView = [TGMActivityIndicatorView defaultActivityIndicatorView];
        activityView.center = CGPointMake(self.boundsCenterX, self.boundsHeight - INDICATOR_CENTER_BOTTOM);
        activityView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin;
        [self addSubview:activityView];
        _activityView = activityView;
    }
    return self;
}

- (void)pullToRefreshView:(SVPullToRefreshView *)pullToRefreshView
          stateDidChanged:(SVPullToRefreshState)state from:(SVPullToRefreshState)previousState
{
    switch (state) {
        case SVPullToRefreshStateLoading:
            _statusLabel.text = @"加载中...";
            [_activityView startAnimating];
            break;
        case SVPullToRefreshStateStopped:
            _statusLabel.text = @"下拉刷新";
//            [_activityView stopAnimating];
            break;
        case SVPullToRefreshStateTriggered:
            _statusLabel.text = @"释放刷新";
//            [_activityView stopAnimating];
            break;
        default:
            break;
    }
}

@end
